// subpackagesA/pages/orderStatistics/orderStatistics.ts
import { getAllVillage } from "../../../api/room-monitoring";
import dayjs from '../../../utils/day'
import {
  villageOrderStatistics,
  buildingOrderStatistics,
} from "../../../api/order";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    option1: [{ text: "全部", value: 0 }],
    value1: 0,
    comID: -1,
    userID: -1,
    villageList: [],
    buildingList: [],
    startShow: false,
    endShow: false,
    startTime: "请选择开始日期",
    endTime: "请选择结束日期",
    villageID: 0,
    minDate: new Date(dayjs().subtract(1, "month")).getTime(),
    maxDate: new Date(dayjs()).getTime(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const comID = wx.getStorageSync("userInfo").data.comID;
    getAllVillage(comID).then((res: any) => {
      const list = res.data.data.map((item: any) => {
        return {
          text: item.villageName,
          value: item.villageID,
        };
      });
      this.setData({
        option1: [...this.data.option1, ...list],
      });
      console.log(this.data.option1)
    });
    this.getVilage();
  },
  getVilage() {
    const startTime =
      this.data.startTime == "请选择开始日期" ? "" : this.data.startTime;
    const endTime =
      this.data.endTime == "请选择结束日期" ? "" : this.data.endTime;
    const comID = wx.getStorageSync("userInfo").data.comID;
    const userID = wx.getStorageSync("userInfo").data.userID
    villageOrderStatistics(comID, startTime, endTime,userID).then((res: any) => {
      if (res.data.length > 0) {
        this.setData({
          villageList: res.data,
        });
      }
    });
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1,
    });
  },
  onItem(e: any) {
    console.log(e);
    const comID = this.data.comID;
    const value = e.detail.value;
    const userID = wx.getStorageSync("userInfo").data.userID
    const startTime =
      this.data.startTime == "请选择开始日期" ? "" : this.data.startTime;
    const endTime =
      this.data.endTime == "请选择结束日期" ? "" : this.data.endTime;
    this.setData({
      villageID: value,
    });
    if (value || value != 0) {
      buildingOrderStatistics(comID, value, startTime, endTime,userID).then(
        (res: any) => {
          if (res.data.length > 0) {
            this.setData({
              buildingList: res.data,
            });
          } else {
            this.setData({
              buildingList: [],
            });
          }
        }
      );
    } else {
      villageOrderStatistics(comID, startTime, endTime,userID).then((res: any) => {
        if (res.data.length > 0) {
          this.setData({
            villageList: res.data,
            buildingList: [],
          });
        }
      });
    }
  },
  onSearch() {
    const villageID = this.data.villageID;
    const comID = this.data.comID;
    const userID = wx.getStorageSync("userInfo").data.userID
    const startTime =
      this.data.startTime == "请选择开始日期" ? "" : this.data.startTime;
    const endTime =
      this.data.endTime == "请选择结束日期" ? "" : this.data.endTime;
    if (villageID || villageID != 0 ) {
      buildingOrderStatistics(comID, villageID, startTime, endTime,userID).then(
        (res: any) => {
          if (res.data.length > 0) {
            this.setData({
              buildingList: res.data,
            });
          } else {
            this.setData({
              buildingList: [],
            });
          }
        }
      );
    }else{
        this.getVilage();
    }
  },
  onClose() {
    this.setData({
      startShow: false,
      endShow: false,
      startTime:'请选择开始日期',
      endTime:'请选择结束日期'
    });
  },
  onStart() {
    this.setData({
      startShow: true,
    });
  },
  onEnd() {
    this.setData({
      endShow: true,
    });
  },
  onStartTime(e: any) {
    this.setData({
      startShow: false,
      startTime: this.formatDate(e.detail),
    });
  },
  onEndTime(e: any) {
    this.setData({
      endShow: false,
      endTime: this.formatDate(e.detail),
    });
  },
  formatDate(date: any) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      comID: wx.getStorageSync("userInfo").data.comID,
      userID: wx.getStorageSync("userInfo").data.userID,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
